<template>
  <div>
    <!-- 主体区域 -->
    <main>
      <div class="w1200">
        <div class="gap"></div>
        <div class="wfd">
          <div class="h160">
            <span><img src="@/assets/imgs/RL_slices/RL.png" alt="" /></span>
            <span class="pdl15">
              <span class="title">{{ eSubjectType.title }}</span>
              <span class="des">{{ eSubjectType.tips }}</span>
            </span>
          </div>
          <div class="h60">
            <span class="exnum">{{ iNum }}.{{ subject.title }}</span>
            <span class="labels">
              <span v-if="subject.isJt == '1'">机经</span>
              <span v-if="subject.isJt !== '1'" style="background: #7cc3d6"
                >练习题</span
              >
              <span
                style="background: #ff5e4e"
                v-if="subject.labelStyle == 'wrong-color'"
                >错题</span
              >
              <span style="background: #13bfbf" v-if="subject.isYc == '1'">本周预测</span>
              <span style="background: #99bbe4" v-if="practiceList.length>0"
                >练习{{ practiceList.length }}次</span
              >
              <span style="background: #94d0f7">{{ subject.titleNumber }}</span>
            </span>
            <Label
              v-if="subject.labelStyle != undefined"
              :df="subject.labelStyle"
              :id="subject.id"
            ></Label>
          </div>
          <div class="h121">
            <div>
              <AudioPlayer
                v-if="subject.url != null"
                :url="subject.url"
                :readyTime="readyTime"
                :answerTime="answerTime"
                :beforeRecordCountdown="beforeRecordCountdown"
                @sendTime="getTime"
                @startRecord="startRecord"
                ref="aplayer"
              ></AudioPlayer>
            </div>
          </div>

          <div class="rl-pic" v-if="subject.imgUrl">
            <img v-lazy="subject.imgUrl" class="pic" />
          </div>

          <div class="time">
            <span class="readyTime" v-if="readyTime > 0"
              >请准备：00:{{
                readyTime > 9 ? readyTime : "0" + readyTime
              }}</span
            >
            <span v-if="readyTime == 0"
              >答题时间：{{ s_to_hs(answerTime) }}</span
            >
            <span>
              准备时间：00：{{
                beforeRecordCountdown > 9
                  ? beforeRecordCountdown
                  : "0" + beforeRecordCountdown
              }}
            </span>
          </div>
        </div>
        <!-- 提示 -->
        <div class="tishi" v-if="!tips">
          <div class="h80">麦克风权限未开启，开启后才能录音！</div>
          <div class="howopen" slot="reference" @click="dialogVisible = true">
            如何开启？
          </div>

          <el-dialog
            title="九炸炸提示"
            :visible.sync="dialogVisible"
            width="50%"
          >
            <div>
              <h1>您的麦克风权限未开启，本题需要此权限！ 请按下列图示操作：</h1>

              <img src="@/assets/imgs/tips.png" alt="" />
            </div>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="dialogVisible = false"
                >确 定</el-button
              >
            </span>
          </el-dialog>
        </div>
        <div class="myanswer">
          <div class="bt">录制答案<span>麦克风开启才可以录制哦~</span></div>
          <div class="insert">
            <div
              class="microphone"
              :class="!isOpen ? 'setpink' : ''"
              @click="handleClick"
            >
              <i class="iconfont icon-maikefeng"></i>
            </div>
            <div v-if="isOpen > 0" class="countdown">点击开始录音</div>
            <div class="countdown" v-else>
              <span ref="zeroHidden">点击结束录音</span>
            </div>
          </div>
          <!-- 录音 -->
          <div class="record">
            <el-form ref="recorderForm">
              <Aplayer v-if="url" :url="url"></Aplayer>
              <span class="cover" v-if="url"></span>
            </el-form>
          </div>
          <div class="btns">
            <el-button @click="addPractice()">提交</el-button>
            <el-button @click="resetRecorderForm()">重做</el-button>
          </div>
        </div>
        <!-- 提示音 -->
          <audio ref="audio" :src="audioUrl"></audio>
        <div class="gap"></div>
        <!-- 搜索，上下题目切换 -->
        <div class="h100">
          <div class="search-box">
            <span
              ref="answer"
              @click="seeAnswer"
              :class="isAnswerShow ? 'show_answer' : ''"
              class="seeAnswer"
              >查看答案</span
            >
            <el-input placeholder="输入内容/题目" v-model="keyword">
              <template slot="append">
                <div @click="searchSubject('readingRFIB')">搜索</div>
              </template>
            </el-input>
            <el-button class="prev" @click="goTo(last, '1', 'readingRFIB',subject.id)"
              >上一题</el-button
            >
            <el-button class="next" @click="goTo(next, '2', 'readingRFIB',subject.id)"
              >下一题</el-button
            >
          </div>
          <div class="answer-box" v-show="isAnswerShow">
            <div class="title">参考答案</div>
            <div class="content">{{ subject.answer }}</div>
            <div class="orig-txt" v-if="subject.originalText">原文</div>
            <div class="orig-content">{{ subject.originalText }}</div>
          </div>
        </div>
        <div class="myexercise">
          <div class="bt">我的练习</div>
          <ul>
            <li v-for="(item, index) in practiceList" :key="index">
              <span class="avatar">
                <img v-lazy="item.image" alt="" />
              </span>
              <span class="content">
                <span>
                  {{ item.nickname }}
                  <span class="time">{{ item.createDate | formatTM }}</span>
                </span>
                <Bplayer :url="item.answer"></Bplayer>
              </span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 做题中选题抽屉 -->
      <Drawer :img="imgUrl"></Drawer>
    </main>
  </div>
</template>

<script>
import writingUtil from "../../../../../utils/writingUtil";
// 引入标签组件
import Label from '../../label'
import Drawer from '../../drawer'
// 引入音频插件
import Aplayer from '../../aplayer'
import AudioPlayer from '../../audioplayer'
import Bplayer from '../../bottom_player'
import { sendPost } from "../../../../../utils/http";
import api from "../../../../../config/api";


export default {
  mixins: [writingUtil],
  components: {
    Aplayer, // 音频组件
    Drawer,
    Label,
    AudioPlayer,
    Bplayer
  },
  data () {
    return {
       audioUrl: require('../../../../../assets/alert/dingd.mp3'), // 提示音
      imgUrl: '',
      countDown: 30,
      recordingCountDown: 40, // 答题时间倒计时
      readyTime: 3,//准备时间
      answerTime: 0,//答题时间
      value: false, // 控制参考答案是否显示
      totalTime: 0, // 录音总时长
      percent: 0, // 播放进度比
      type: 'pause',
      answer: "123",//答案
      subject: {},//题目详情
      eSubjectType: {},//题型信息
      practiceList: [],//答题记录
      keyword: '',//搜索关键字
      last: '',//上一题
      next: '',//下一题
      url: '',//录音播放
      id: '',
      interval: {},//循环组件
      isOpen: false,//是否开麦克风
      tips: false, // 麦克风是否授权
      dialogVisible: false,
      isAnswerShow: false,
      iNum: '', // 题目索引号
      beforeRecordCountdown: 10
    }
  },

  created () {
    var query = this.$route.query;
    this.getData(query.id);
    this.coutDown()
    // 进入页面即请求麦克风权限
    this.getPermission()
    this.answerTimeCount()
   this.iNum = this.$route.query.idx;
  },
  mounted () {
    sessionStorage.setItem('activeIndex', 'loged')
  },
  beforeDestroy () {
    clearInterval(this.interval.answerTime)
    this.url = ''
  },
  destroyed () {
    var that = this
    that.getRecorder();
    this.url = ''
  },
  watch: {
    readyTime: {
      handler () {
        // 天天改尼玛需求
        var that = this
        if (this.readyTime == 0 && this.beforeRecordCountdown == 0) {
          // console.log(1);
          // that.startRecorder()
          // this.answerTimeCount()
        }
      }
    },
    // 监听录音播放完毕后倒计时
    beforeRecordCountdown () {
      var that = this
      if (that.beforeRecordCountdown == 0) {
        this.$refs.audio.play();//点击触发 点击音效
       setTimeout(() => {
          this.isOpen = false
        that.startRecorder()  // 倒计时结束，开始录音，计时
        this.answerTimeCount()
       }, 2000);
      }
    }
  },
  methods: {
  
    // 录音播放完毕，倒计时10s后，自动开始录音
    startRecord () {
      var that = this
      setInterval(() => {
        if (that.beforeRecordCountdown > 0) {
          that.beforeRecordCountdown--
        }
      }, 1000);
    },
    // 播放音频的时候
    getTime () {
      clearInterval(this.interval.answerTime)
      this.answerTime = 0
      this.readyTime = 0
    },
    seeAnswer () {
      this.isAnswerShow = !this.isAnswerShow
      if (this.isAnswerShow) {
        this.$refs.answer.innerText = '隐藏答案'
      } else {
        this.$refs.answer.innerText = '查看答案'
      }
    },
    // 重做题目
    resetRecorderForm () {
      // clearInterval(this.interval.answerTime)
      // clearInterval(this.interval.mkf)
      // this.readyTime = 3
      // this.url = ''
      // this.isOpen = true
      // this.answerTimeCount()
      // this.coutDown()
      this.$router.go(0)
    },
    // 点击麦克风
    handleClick () {
      // 获取了麦克风权限才允许点击麦克录音
      var that = this
      if (this.tips) {
        this.isOpen = !this.isOpen
        if (this.beforeRecordCountdown > 0) {
          this.isOpen = true
        }
        if (!this.isOpen) { // 开启录音
          this.readyTime = 0
          this.$refs.audio.play();//点击触发 点击音效
         setTimeout(() => {
            clearInterval(this.interval.answerTime)
          if (this.answerTime !== 0) {
            this.answerTime = 0
            this.answerTimeCount()
            // this.isOpen = !this.isOpen
            // this.startRecorder()
          }
          that.url = '' // 再次录音的时候，把播放器文件清空
         }, 500);
        } else { // 关闭录音
          this.getRecorder()
          // this.isOpen = !this.isOpen
          clearInterval(this.interval.answerTime)
        }
      } else {
        this.$msg.info('请先打开麦克风授权')
      }
    },
    // 准备答题倒计时
    // 准备答题倒计时
    coutDown () {
      var that = this
      this.interval.mkf = setInterval(() => {
        if (this.readyTime > 0) {
          this.readyTime--
        } else {
          // this.isOpen = false
        }
      }, 1000);
    },
    // coutDown () {
    //   var that = this
    //   this.interval.mkf = setInterval(() => {
    //     if (this.readyTime > 0) {
    //       this.readyTime--
    //     } else {
    //       if (this.answerTime == 0) {
    //         return
    //       } else {
    //         this.isOpen = false
    //       }
    //     }
    //   }, 1000);
    // },
    // 答题时间计时
    answerTimeCount () {
      clearInterval(this.interval.answerTime)
      this.answerTime = 0
      this.interval.answerTime = setInterval(() => {
        // 答题时间上限
        if (this.answerTime < 40) {
          this.answerTime++
        } else {
          this.isOpen = true
          this.getRecorder()
          clearInterval(this.interval.answerTime)
        }
      }, 1000);


    },

   //跳转题目页面
    goTo (id, type, name, nowId) {
      console.log("搜索中", type, id, name, nowId)
      // 没有搜索到题
      if (id == '0') {
        if (type == '1') {
          this.$msg({
            message: '没有上一题了',
            type: 'warning'
          });
        }
        if (type == '2') {
          this.$msg({
            message: '没有下一题了',
            type: 'warning'
          });
        }
        return
        // 搜到题
      }
      else {
        if (nowId > id) {
          // console.log(nowId,id);
          this.$router.push('/writingRL?id=' + id + '&idx=' + (this.iNum / 1 - 1));

        } else if (nowId < id) {
          console.log(nowId, id);
          this.$router.push('/writingRL?id=' + id + '&idx=' + (this.iNum / 1 + 1));
        }
      }
    },
    //搜索题目
    searchSubject () {
      var that = this
      sendPost(api.searchSubject, {
        keyword: that.keyword,
        subjectTypeId: that.eSubjectType.id
      }).then((res) => {
        if (res.data != null) {
          if (res.data.id) {
            this.$router.push('/writingRL?id=' + res.data.id);
            location.reload()
          }
        } else {
          this.$msg({
            message: '没有搜到题目',
            type: 'warning'
          });
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.setpink {
  background-color: #ff5e4e !important;
}
.readyTime {
  color: #ff5e4e;
}
/deep/.drawerbtn {
  position: fixed;
  top: 50%;
  right: 2.8125rem;
  z-index: 3000;
  width: 5.625rem;
  height: 5.625rem;
  background: #ff5e4e;
  box-shadow: 0px 0px 1.25rem 0px #ff5e4e;
  border-radius: 50%;
  transform: translate(50%, 50%);
  font-size: 1rem;
  border: none;
  .iconfont {
    font-size: 1.125rem;
  }
  .txt {
    margin-top: 0.1875rem;
  }
}
/deep/.el-drawer__header {
  margin-bottom: 0;
  padding: 0;
}
/deep/.el-drawer__body {
  margin-top: 1.875rem;
  padding: 0 3.75rem;
  box-sizing: border-box;
}
.el-drawer {
  .title {
    font-size: 2rem;
    color: #333;
    font-weight: 400;
    line-height: 5rem;
    display: flex;
    .wh80 {
      display: inline-block;
      width: 5rem;
      height: 5rem;
      background-color: #d3d3d3;
      margin-right: 1.25rem;
    }
  }
  .el-input {
    width: 20rem;
  }
}
/deep/.el-tabs__active-bar {
  height: 0.25rem;
}
/deep/.el-tabs__item.is-active {
  color: #333;
  border-bottom-color: #ff5e4e;
}
/deep/.el-tabs__item:hover {
  color: #333;
}
/deep/.el-tabs__active-bar {
  background-color: #ff5e4e;
}
/deep/.el-tabs__nav-wrap::after {
  background-color: #fff;
}
/deep/.el-tabs__nav {
  padding: 0.4375rem 0;
}
/deep/.el-tabs__item {
  font-size: 1rem;
}
/deep/.el-input__inner {
  border-radius: 1.25rem;
  border-color: #c0c4cc;
}
/deep/.el-col-8 {
  margin-top: 0.75rem;
}
main {
  height: 100%;
  width: 100%;
  background-color: #eff1f4 !important;
  padding-bottom: 4.625rem;
  box-sizing: border-box;
  .w1200 {
    width: 75rem;
    margin: auto;
    background-color: #eff1f4;
    .gap {
      height: 0.75rem;
    }
    .wfd {
      background-color: #fff;
      padding: 2.5rem 2.5rem 1.875rem;
      box-sizing: border-box;
      .h160 {
        height: 10rem;
        display: flex;
        padding-right: 6.875rem;
        box-sizing: border-box;
        .pdl15 {
          padding-left: 0.9375rem;
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          .title {
            font-size: 1.25rem;
            color: #333;
            font-weight: bold;
          }
          .des {
            font-size: 1rem;
            color: #999;
            line-height: 2rem;
          }
        }
      }
      .h60 {
        height: 3.75rem;
        line-height: 3.75rem;
        padding-left: 1.25rem;
        box-sizing: border-box;
        background-color: #f7f7f7;
        display: flex;
        position: relative;
        .exnum {
          width: 45rem;
          font-size: 1rem;
          color: #666;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .labels {
          display: flex;
          justify-content: flex-end;
          width: 21.25rem;
          span {
            height: 1.875rem;
            line-height: 1.875rem;
            padding: 0 0.375rem;
            box-sizing: border-box;
            background-color: #ff9b4f;
            border-radius: 0.1875rem;
            margin: 0.9375rem 0.46875rem;
            color: #fff;
            box-sizing: border-box;
          }
        }
      }
      .h121 {
        margin: 3.125rem 0 1.5625rem;
        width: 100%;
        background: #fffafa;
        border: 0.1875rem dashed #ff5e4e;
        border-bottom: 0.1875rem dashed #ff5e4e !important;
        box-sizing: border-box;
        padding: 0rem 1.75rem 1.6875rem;
        box-sizing: border-box;
        font-size: 1.125rem;
        color: #666;
        line-height: 2rem;
        padding-top: 2.625rem;
        div {
          display: flex;
          .iconfont {
            font-size: 1.75rem;
            color: #666;
            cursor: pointer;
          }
          span {
            margin: auto;
          }
        }
        /deep/.el-progress__text i {
          display: none;
        }
        /deep/.el-progress-bar__outer {
          width: 54.5625rem;
        }
      }
      .time {
        font-size: 1rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
        position: relative;
        span {
          margin-right: 1.875rem;
        }
        .iconfont {
          font-size: 1.5rem;
          color: #ccc;
        }
        .end {
          position: absolute;
          right: -1.5rem;
          text-align: right;
        }
      }
    }
    .tishi {
      height: 10.0625rem;
      background-color: #ff5e4e;
      margin-top: 0.625rem;
      text-align: center;
      color: #fff;
      .h80 {
        height: 5rem;
        line-height: 5rem;
        font-size: 1.5rem;
      }
      .howopen {
        width: 12.5rem;
        height: 3.125rem;
        line-height: 3.125rem;
        border: 0.0625rem solid #ffffff;
        border-radius: 0.1875rem;
        margin: auto;
        font-size: 1rem;
        cursor: pointer;
      }
    }
    .myanswer {
      height: 24rem;
      margin-top: 0.625rem;
      background-color: #fff;
      padding: 0 2.5rem;
      box-sizing: border-box;
      position: relative;
      .bt {
        height: 7.5rem;
        line-height: 7.5rem;
        font-size: 2rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
        span {
          color: #999;
          font-size: 1rem;
          margin-left: 0.625rem;
        }
      }
      .txt {
        height: 13.125rem;
        background-color: #f7f7f7;
        border: none;
        outline: none;
        padding: 1.875rem;
        box-sizing: border-box;
        font-size: 1.125rem;
        color: #999999;
        resize: none;
        width: 100%;
      }
      .insert {
        .microphone {
          width: 4.375rem;
          height: 4.375rem;
          line-height: 4.375rem;
          text-align: center;
          margin-left: 47%;
          border-radius: 50%;
          cursor: pointer;
          background-color: #eee;
          .iconfont {
            font-size: 2.5rem;
            color: #fff;
          }
        }
        .countdown {
          height: 2.8125rem;
          line-height: 2.8125rem;
          font-size: 1rem;
          color: #333;
          text-align: center;
        }
      }
      .record {
        width: 420px;
        margin: 0 auto;
        position: relative;
        .cover {
          position: absolute;
          width: 1.25rem;
          height: 1.25rem;
          top: 1.5625rem;
          right: 2.8125rem;
          background-color: #f2f2f2;
        }
      }
      .btns {
        height: 4.125rem;
        width: 20.875rem;
        margin: auto;
        line-height: 2.5rem;

        .el-button {
          color: #ff5e4e;
          font-size: 1.125rem;
          text-align: center;
          width: 5rem;
          border-radius: 0.3125rem;
          border: 0.0625rem solid #ff5e4e;
          margin: 0.625rem 2.5rem;
          background-color: #fffafa;
        }
      }
    }
    .myexercise {
      margin-top: 0.625rem;
      background-color: #fff;
      padding: 0 2.5rem;
      box-sizing: border-box;
      padding-bottom: 1.25rem;
      .no-data {
        width: 16.875rem;
        height: 11.25rem;
        margin: auto;
        text-align: center;
        color: #999;
        margin-bottom: 2.5rem;
        .iconfont {
          font-size: 6.875rem;
        }
      }
      .bt {
        height: 6.25rem;
        line-height: 6.25rem;
        font-size: 2rem;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #333333;
      }
      ul {
        li {
          display: flex;
          padding-bottom: 1.5rem;
          margin-bottom: 1.2rem;
          border-bottom: 1px solid #ccc;
          .avatar {
            width: 3rem;
            height: 3rem;
            border-radius: 50%;
            margin-right: 0.625rem;
            img {
              width: 100%;
              height: 100%;
              border-radius: 50%;
            }
          }
          .content {
            flex: 1;
            font-size: 1.5rem;
            color: #333;
            display: flex;
            flex-direction: column;
            .time {
              font-size: 1rem;
              color: #999;
              margin-left: 1.875rem;
            }
            .title {
              margin-bottom: 1rem;
            }
          }
        }
      }
    }
    .h100 {
      padding: 1.5625rem 2.5rem;
      background-color: #fff;
      .el-button {
        color: #ff5e4e;
        font-size: 1.125rem;
        text-align: center;
        width: 5rem;
        border-radius: 0.3125rem;
        border: 0.0625rem solid #ff5e4e;
        background-color: #fffafa;
        margin-right: 1.25rem;
      }
      .el-switch {
        margin-right: 0.5rem;
        padding-left: 7.5rem;
      }
      .el-input {
        width: 14.875rem;
        margin-left: 7.25rem;
        margin-top: 8px;
        ::placeholder {
          font-size: 1.125rem;
          color: #cccccc;
        }
      }
      /deep/.el-input-group__append {
        padding: 0 1rem;
        background-color: #ff5e4e;
        color: #fff;
        border: 0.0625rem solid #ff5e4e;
        box-sizing: border-box;
        font-size: 1.125rem;
        cursor: pointer;
      }
      /deep/.el-input__inner {
        height: 3.125rem;
        width: 14.875rem;
        border-radius: 0.3125rem 0 0 0.3125rem;
        overflow: hidden;
        border-color: #ededed;
        border-right: none;
      }
      .prev {
        margin-left: 3rem;
      }
      .prev,
      .next {
        width: 5rem;
        background: #fffafa;
        border: 0.0625rem solid #ff5e4e;
        box-sizing: border-box;
        border-radius: 0.3125rem;
        color: #ff5e4e;
        margin-top: 0.8125rem;
        // margin-left: 2.5rem;
        padding-left: 0.75rem;
        font-size: 1.125rem;
      }
      .answer-box {
        .title {
          font-size: 2rem;
          color: #333;
          height: 5.3125rem;
          line-height: 5.3125rem;
        }
        .content {
          font-size: 1.125rem;
          color: #666;
          line-height: 2rem;
        }
      }
    }
  }
}
.icon-lingdang1 {
  color: #eee;
  line-height: 3.75rem;
}
.avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  display: inline-block;
  vertical-align: middle;
}
.username {
  display: inline-block;
  margin-left: 0.625rem;
  font-weight: bold;
  font-size: 1rem;
  color: #333333;
  width: 6.25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.jj-bg {
  background: #ff9b4f;
}
.jj-color {
  color: #ff9b4f;
}
.lxt-bg {
  background: #7cc3d6;
}
.lxt-color {
  color: #7cc3d6;
}
.wrong-bg {
  background: #ff5e4e;
}
.wrong-color {
  color: #ff5e4e;
}
.gpzt-bg {
  background: #13bfbf;
}
.gpzt-color {
  color: #13bfbf;
}
.lxcs-bg {
  background: #99bbe4;
}
.lxcs-color {
  color: #99bbe4;
}
.ylx-bg {
  background: #94d0f7;
}
.ylx-color {
  color: #94d0f7;
}

.seeAnswer {
  display: inline-block;
  background: #fffafa;
  border: 0.0625rem solid #ff5e4e;
  color: #ff5e4e;
  height: 2.8125rem;
  font-size: 1.125rem;
  line-height: 2.8125rem;
  border-radius: 0.3125rem;
  padding: 0 0.2875rem;
  margin-right: 14.375rem;
  margin-left: 7.25rem;
  cursor: pointer;
}
.show_answer {
  color: #fff;
  background-color: #ff5e4e;
}

.orig-txt {
  font-size: 1.5rem;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #cccccc;
}

.orig-content {
  font-size: 1.125rem;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #666666;
  line-height: 2rem;
}

.rl-pic {
  width: 70rem;
  height: 32rem;
  .pic {
    width: 100%;
    height: 30rem;
  }
}
</style>
